package com.wust.supermarket.controller;

import com.wust.supermarket.domain.Merch;
import com.wust.supermarket.service.MemberService;
import com.wust.supermarket.service.MerchService;
import com.wust.supermarket.service.SaleService;
import com.wust.supermarket.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private MerchService merchService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private SaleService saleService;

    @GetMapping("/")
    public String index(HttpServletRequest request, Model model) {
        int merchNumber = merchService.getMerchNumber();
        int memberNumber = memberService.getMemberNumber();
        int staffNum = staffService.getStaffNum();
        float allSalePrice = saleService.getAllSalePrice();
        List<Merch> merches = merchService.listAllMerches();
        List<Merch> warningMerches = new ArrayList<Merch>();
        for (Merch m : merches) {
            int upnum = m.getUpperNumber();
            int lownum = m.getLowerNumber();
            if (m.getMerchNumber() >= upnum || m.getMerchNumber() <= lownum) {
                warningMerches.add(m);
            }
        }
        System.out.println(warningMerches);
        request.getSession().setAttribute("warningMerches", warningMerches);
        model.addAttribute("wMerches", warningMerches);
        model.addAttribute("merchNumber", merchNumber);
        model.addAttribute("memberNumber", memberNumber);
        model.addAttribute("staffNum", staffNum);
        model.addAttribute("allSalePrice", allSalePrice);
        return "index";
    }
}
